<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Metrics\Examples;

use App\Models\CombustionRecord;
use App\Models\DailyBeanReleases;
use App\Models\OrderBeanRewards;
use App\Models\UserBeanPackages;
use App\Service\Setting\BeanSettingService;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Donut;

class NewDevices extends Donut
{
    protected $labels = [ '已产出', '已燃烧'];

    /**
     * 渲染模板
     *
     * @return string
     */
    public function render()
    {
        $this->fill();

        return parent::render();
    }

    /**
     * 写入数据.
     */
    public function fill()
    {

        $combust = CombustionRecord::query()->select('quantity')->get()->sum('quantity');
        $y = round(BeanSettingService::make()->getSetting('CIRCULATION'),2);
        $r = round($combust, 2);
        $this->withContent(
            $y,
            $r
        );

        // 图表数据
        $this->withChart([
            $y, $r,
        ]);
    }

    /**
     * 设置图表数据.
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 初始化卡片内容.
     */
    protected function init()
    {
        parent::init();

        $color = Admin::color();
        $colors = [$color->primary(), $color->alpha('blue2', 0.5)];

        $this->title('KC通证统计');
        $this->subTitle('释放比例');
        $this->chartLabels($this->labels);
        // 设置图表颜色
        $this->chartColors($colors);
    }

    /**
     * 设置卡片头部内容.
     *
     * @param mixed $desktop
     * @param mixed $mobile
     * @param mixed $a
     * @param mixed $b
     * @param mixed $c
     *
     * @return $this
     */
    protected function withContent($a, $b)
    {

        $green = Admin::color()->alpha('green', 0.5);
        $red = Admin::color()->alpha('red', 0.5);

        $style = 'margin-bottom: 8px';
        $labelWidth = 120;

        return $this->content(
            <<<HTML
<div class="d-flex pl-1 pr-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle" style="color: {$green}"></i> {$this->labels[0]}
    </div>
    <div>{$a}</div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle" style="color: {$red}"></i> {$this->labels[1]}
    </div>
    <div>{$b}</div>
</div>
HTML
        );
    }
}
